package com.xh.bussiness.xkw.controller;


import com.xh.bussiness.system.model.Dictionary;
import com.xh.bussiness.tiku.bean.dto.QuestionDto;
import com.xh.bussiness.xkw.bean.request.QuestionSearchRequest;
import com.xh.bussiness.xkw.model.XkwQuestion;
import com.xh.bussiness.xkw.model.XkwSubject;
import com.xh.bussiness.xkw.service.*;
import com.xh.bussiness.xkw.service.impl.XkwQuestionServiceImpl;
import com.xh.bussiness.xkw.utils.Pic2Base64Utils;
import com.xh.core.bean.AjaxResult;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/xkw/api")
public class XkwApiController {

    @Autowired
    private IXkwQuestionService xkwQuestionService;
    @Autowired
    private IXkwSubjectService xkwSubjectService;
    @Autowired
    private IXkwCategoryService xkwCategoryService;
    @Autowired
    private IXkwVersionBookService xkwVersionBookService;
    @Autowired
    private IXkwChapterService xkwChapterService;
    @Autowired
    private IXkwChapterCategoryService xkwChapterCategoryService;
    @Autowired
    private IXkwPaperTypeService xkwPaperTypeService;
    @Autowired
    private IXkwQuesTypeService xkwQuesTypeService;

    @Autowired
    private IXkwSysDictionaryService dictionaryService;
    @Autowired
    private  IXkwTiKuKnowledgeService knowledgeService;
    /**
     * 查询试题
     * @param uuid 通过唯一id查询添加的学科网试题
     * @return
     */
    @RequestMapping("/queryQuestionLog")
    public AjaxResult queryQuestionLog(String uuid){
        if(null == uuid){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"参数错误" );
        }

        List questionList;
        try {
            questionList = xkwQuestionService.queryQuestion(uuid);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"系统错误，请稍候再试！");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,questionList);
    }

    /**
     * 拍图搜题
     * @param filePath  图片路径
     * @return
     */
    @RequestMapping("/queryQuestionByImg")
    public AjaxResult queryQuestionByImg(String filePath, HttpServletRequest request){
//        filePath = "/home/thev/图片/2021-01-04_17-30.png";
        if (StringUtils.isEmpty(filePath)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        String realPath =request.getSession().getServletContext().getRealPath("/") + filePath;

        System.out.println(realPath);

        String encodeImg = Pic2Base64Utils.Pic2EncodeStr(realPath, true);
        System.out.println(encodeImg);
        List<QuestionDto> questionList = xkwQuestionService.queryQuestionByImg(encodeImg);

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, questionList);
    }

    /**
     * 同步科目
     * @return
     */
    @RequestMapping("/sysSubject")
    public String  SysSubject(){

        try {
            List<XkwSubject> xkwSubjectList = xkwSubjectService.SysXkwSubject();
        } catch (Exception e) {
            e.printStackTrace();
            return "学科、同步失败！" + e.getMessage();
        }
        return "学科同步完成！";
    }

    /**
     * 同步年级
     * @return
     */
    @RequestMapping("/sysGrade")
    public String sysGrade(){

        try {
            xkwSubjectService.SysXkwGrade();
        } catch (Exception e) {
            e.printStackTrace();
            return "学科、同步失败！" + e.getMessage();
        }
        return "年级同步完成！";
    }

    /**
     * 同步题目来源
     * @return
     */
    @RequestMapping("/sysQuestionSource")
    public String sysQuestionSource(){

        try {
            xkwSubjectService.sysQuestionSource();
        } catch (Exception e) {
            e.printStackTrace();
            return "学科、同步失败！" + e.getMessage();
        }
        return "题目来源同步完成！";
    }

    /**
     * 同步题目难度
     * @return
     */
    @RequestMapping("/sysQuestionDiff")
    public String sysQuestionDiff(){

        try {
            xkwSubjectService.sysQuestionDifficult();
        } catch (Exception e) {
            e.printStackTrace();
            return "学科、同步失败！" + e.getMessage();
        }
        return "题目难度同步完成！";
    }


    /**
     * 同步知识点
     * @return
     */
    @RequestMapping("/sysCategory")
    public String  SysCategory(){

        try {
            xkwCategoryService.SysXkwCategories();
        } catch (Exception e) {
            e.printStackTrace();
            return "知识点同步失败！" + e.getMessage();
        }
        return "知识点同步完成！";
    }

    /**
     * 同步版本教材
     * @return
     */
    @RequestMapping("/sysXkwVersionBook")
    public String  sysXkwVersionBook(){

        try {
            xkwVersionBookService.sysXkwVersionBook();
        } catch (Exception e) {
            e.printStackTrace();
            return "版本教材同步失败！" + e.getMessage();
        }
        return "版本教材同步完成！";
    }

    /**
     * 同步章节
     * @return
     */
    @RequestMapping("/sysXkwChapters")
    public String  sysXkwChapters(){

        try {
            xkwChapterService.sysXkwChapters();
        } catch (Exception e) {
            e.printStackTrace();
            return "章节同步失败！" + e.getMessage();
        }
        return "章节同步完成！";
    }


    /**
     * 同步章节下的知识点
     * @return
     */
    @RequestMapping("/sysXkwCategoryByChapter")
    public String  sysXkwCategoryByChapter(){

        try {
            xkwChapterCategoryService.sysXkwCategoryByChapter();
        } catch (Exception e) {
            e.printStackTrace();
            return "同步章节下的知识点失败！" + e.getMessage();
        }
        return "同步章节下的知识点完成！";
    }

    /**
     * 同步试题类型
     * @return
     */
    @RequestMapping("/sysXkwQuestionType")
    public String  sysXkwQuestionType(){

        try {
            xkwQuesTypeService.sysXkwQuestionType();
        } catch (Exception e) {
            e.printStackTrace();
            return "同步试题类型失败！" + e.getMessage();
        }
        return "同步试题类型完成！";
    }

    /**
     * 同步试卷类型
     * @return
     */
    @RequestMapping("/sysXkwPaperType")
    public String  sysXkwPaperType(){

        try {
            xkwPaperTypeService.sysXkwPaperType();
        } catch (Exception e) {
            e.printStackTrace();
            return "同步试卷类型失败！" + e.getMessage();
        }
        return "同步试卷类型完成！";
    }

    /**
     * 初始化学科网试题类型关系
     * @return
     */
    @RequestMapping("/initQuesitonType")
    public String  initQuesitonType(){

        try {
//            xkwQuestionServiceImpl.initQuestionType();
        } catch (Exception e) {
            e.printStackTrace();
            return "初始化学科网试题类型关系失败！" + e.getMessage();
        }
        return "初始化学科网试题类型关系完成！";
    }


    /**
     * 查询学科
     * @author 赵文博
     * @date 2021/5/20 15:41
     * @return com.xh.core.bean.AjaxResult
     **/
    @GetMapping("/querySubject")
    public AjaxResult querySubject(){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,dictionaryService.querySubject());
    }

    /**
     * 根据学科查询知识点
     * @author 赵文博
     * @date 2021/5/20 15:44
     * @param subjectCode
     * @return com.xh.core.bean.AjaxResult
     **/
    @GetMapping("/queryKnowledge")
    public AjaxResult queryKnowledgeBySubjectCode(String subjectCode){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,knowledgeService.queryKnowledgeBySubjectCode(subjectCode));
    }

    /**
     * 根据科目随机查询试题
     **/
    @RequestMapping("/queryQuestionRandom")
    public AjaxResult queryQuestionRandom(QuestionSearchRequest request){
        if(null == request.getBankId()){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"参数错误！");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,xkwQuestionService.queryQuestionRandom(request));
    }

    /**
     * 根据科目、关键字查询试题
     **/
    @RequestMapping("/queryQuestionSearch")
    public AjaxResult queryQuestionSearch(QuestionSearchRequest request){
        if(null == request.getBankId() || StringUtils.isEmpty(request.getSearchKeyword())){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"参数错误！");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,xkwQuestionService.queryQuestionByKeyword(request));
    }
}
